---------------
-- 福利礼包
-- type    当前类型
-- period  当前阶段
-- cnt     剩余次数

local Util = require("common.Util")
local Log = require("common.Log")
local Lang = require("common.Lang")
local Msg = require("core.Msg")
local ObjHuman = require("core.ObjHuman")
local WelfareGiftExcel = require("excel.present").welfareGift
local BuyExcel = require("excel.buy")
local BuyLogic = require("topup.BuyLogic")
local HeroLogic = require("hero.HeroLogic")
local BagLogic = require("bag.BagLogic")
local Grid = require("bag.Grid")
local Broadcast = require("broadcast.Broadcast")
local UnionDBLogic = require("union.UnionDBLogic")
local MailManager = require("mail.MailManager")
local MailExcel = require("excel.mail")

local WELFACE_GIFT_EXCEL = {}
local WELFACE_GIFT_LEN = 0

function initAfterHot()
   for k, v in ipairs(WelfareGiftExcel) do
       local type = v.type
       if not WELFACE_GIFT_EXCEL[type] then
           WELFACE_GIFT_LEN = WELFACE_GIFT_LEN + 1
       end
       WELFACE_GIFT_EXCEL[type] = WELFACE_GIFT_EXCEL[type] or {}
       WELFACE_GIFT_EXCEL[type][v.period] = k
   end
end

function query(human)
    local welfareGift = human.db.welfareGift
    local giftIndex = welfareGift.giftIndex
    if not giftIndex then return end

	local msgRet = Msg.gc.GC_WELFARE_GIFT_QUERY
    local now = os.time()
    local diffTime = Util.getNextDayTime(now, 1)
    msgRet.endTime = diffTime - now
    local len = 0

    for k, v in pairs(WELFACE_GIFT_EXCEL) do
        welfareGift[k] = welfareGift[k] or {}

        local welfare = welfareGift[k]
        local startIndex = 1
        local period = welfare.period or startIndex
        local config = WelfareGiftExcel[v[period]]
        if giftIndex == config.type then
           period = welfare.period or 0
           config = WelfareGiftExcel[v[period]]
        end

        local cnt = welfare.cnt or config.cnt
        len = len + 1
        local net = msgRet.list[len]
        net.id = v[period]
        net.name = config.name
        net.cnt = cnt
        net.cntMax = config.cnt
        net.price = config.price
        net.oldPrice = config.oldPrice
        net.desc = config.desc

        local rewardLen = 0
        for _id, data in ipairs(config.reward) do
           rewardLen = rewardLen + 1
           Grid.makeItem(net.items[rewardLen], data[1], data[2])
        end
        net.items[0] = rewardLen
    end
    msgRet.list[0] = len 
	Msg.send(msgRet,human.fd)
end

function buy(human, id)
    local config = WelfareGiftExcel[id]
    if not config then return end

    local welfareGift = human.db.welfareGift
    local welfare = welfareGift[config.type]
    if not welfare then return end

    local giftIndex = welfareGift.giftIndex
    if not giftIndex then return end

    local startIndex = 1
    if giftIndex == config.type then
       startIndex = 0
    end

    -- 不是同一级礼包
    local period = welfare.period or startIndex
    if period ~= config.period then return end

    local cnt = welfare.cnt or config.cnt
    if cnt <= 0 then return end

    -- 钻石判断
    local needZuanshi = config.price
    if human.db.zuanshi < needZuanshi then
        return Broadcast.sendErr(human, Lang.COMMON_NO_ZUANSHI)
    end
    -- 扣钻石
    ObjHuman.decZuanshi(human, - needZuanshi, "buySpecific")

    BagLogic.cleanMomentItemList()
    for _id, data in ipairs(config.reward) do
        BagLogic.updateMomentItem(BagLogic.ADDITEM_TYPE_1, data[1], data[2])
    end

    cnt = cnt - 1
    welfare.cnt = cnt
    if welfare.cnt <= 0 then
       period = period + 1
       if not WELFACE_GIFT_EXCEL[config.type][period] then 
          query(human)
          return 
       end
       welfare.period = period
    end


    local items = {}
    local len = 0
    for k, v in ipairs(config.unionReward) do	
        len = len + 1
        local itemID = v[1]
        local itemCnt = v[2]
	    items[len] = { itemID, itemCnt }
    end

    for k,v in pairs(items) do
        BagLogic.updateMomentItem(BagLogic.ADDITEM_TYPE_1, v[1],v[2])
    end
    BagLogic.addMomentItemList(human, "buySpecific")
    query(human)
end


function updateDaily(human)
   local ramdomIndex = math.random(1, WELFACE_GIFT_LEN)
   human.db.welfareGift = {giftIndex = ramdomIndex}
end